package com.ptp.backendcertms.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ptp.backendcertms.api.external.dto.req.CertificateDeleteReqDto;
import com.ptp.backendcertms.api.external.dto.req.CertificateListReqDto;
import com.ptp.backendcertms.api.external.dto.req.CertificateUpdateReqDto;
import com.ptp.backendcertms.api.external.dto.res.CertificateDetailResDto;
import com.ptp.backendcertms.api.external.dto.res.CertificateInfoResDto;
import com.ptp.backendcertms.api.external.dto.res.CertificateListResDto;
import com.ptp.backendcertms.api.external.dto.res.PageInfoDto;
import com.ptp.backendcertms.api.internal.constant.cert.CertificateStatusEnum;
import com.ptp.backendcertms.basiccompnents.data.dao.entity.CertificatePo;
import com.ptp.backendcertms.basiccompnents.data.dao.service.ICertificateService;
import com.ptp.backendcertms.basiccompnents.properties.ProjectProperties;
import com.ptp.backendcertms.business.handler.PageInfoCommonHandler;
import com.ptp.backendcertms.business.service.ICertificateBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 证书 业务服务实现
 *
 * @author songcongcong
 * @date 2025/5/3
 */
@Service
public class CertificateBusinessServiceImpl implements ICertificateBusinessService {
    @Resource
    private ICertificateService certificateService;
    @Resource
    private PageInfoCommonHandler pageInfoCommonHandler;
    @Resource
    private ProjectProperties projectProperties;

    @Override
    public CertificateListResDto getCertificateList(CertificateListReqDto reqDto) {
        PageInfoDto pageInfoDto = pageInfoCommonHandler.buildPageInfo(0L, reqDto.getCurrent(), reqDto.getPageSize());
        CertificateListResDto resDto = new CertificateListResDto();
        resDto.setPageInfo(pageInfoDto);
        if (StringUtils.isNotBlank(reqDto.getOrganizationName()) && Arrays.stream(projectProperties.getTemplate().getOrgNameShield()).anyMatch(t -> reqDto.getOrganizationName().equals(t))) {
            return resDto;
        }
        CertificatePo queryPo = new CertificatePo();
        queryPo.setOrganizationName(reqDto.getOrganizationName());
        queryPo.setSocialCreditCode(reqDto.getSocialCreditCode());
        queryPo.setCertificateNo(reqDto.getCertificateNo());
        if (StringUtils.isNotBlank(reqDto.getCertificateStatus())) {
            queryPo.setCertificateStatus(CertificateStatusEnum.valueOf(reqDto.getCertificateStatus()));
        }
        // 分页查询证书列表
        IPage<CertificatePo> certPoPage = certificateService.pageListMc(queryPo, reqDto.getDueStartDate(), reqDto.getDueEndDate(), reqDto.getIssueStartDate(), reqDto.getIssueEndDate(), reqDto.getCurrent(), reqDto.getPageSize());
        resDto.getPageInfo().setTotal(certPoPage.getTotal());
        // 封装证书列表
        List<CertificateInfoResDto> list = certPoPage.getRecords().stream().map(t -> {
            CertificateInfoResDto orgCertInfoResDto = new CertificateInfoResDto();
            // 流水号
            orgCertInfoResDto.setSerialNo(t.getSerialNo());
            // 组织名称
            orgCertInfoResDto.setOrganizationName(t.getOrganizationName());
            // 证书编号
            orgCertInfoResDto.setCertificateNo(t.getCertificateNo());
            // 证书到期日期
            orgCertInfoResDto.setDueDate(t.getDueDate());
            // 证书状态
            orgCertInfoResDto.setCertificateStatus(t.getCertificateStatus().name());
            // 证书状态描述
            orgCertInfoResDto.setCertificateStatusDesc(t.getCertificateStatus().getValue());

            // 颁证日期
            orgCertInfoResDto.setIssueDate(t.getIssueDate());
            // 统一社会信用代码
            orgCertInfoResDto.setSocialCreditCode(t.getSocialCreditCode());

            return orgCertInfoResDto;
        }).collect(Collectors.toList());
        resDto.setList(list);
        return resDto;
    }

    @Override
    public CertificateDetailResDto getCertificateDetail(String serialNo) {
        CertificatePo certificatePo = certificateService.queryBySerialNo(serialNo);
        CertificateDetailResDto resDto = new CertificateDetailResDto();
        // 流水号
        resDto.setSerialNo(certificatePo.getSerialNo());
        // 组织名称
        resDto.setOrganizationName(certificatePo.getOrganizationName());
        // 认证项目
        resDto.setCertificationProject(certificatePo.getCertificationProject());
        // 证书编号
        resDto.setCertificateNo(certificatePo.getCertificateNo());
        // 证书到期日期
        resDto.setDueDate(certificatePo.getDueDate());
        // 证书状态
        resDto.setCertificateStatus(certificatePo.getCertificateStatus().name());
        // 证书状态描述
        resDto.setCertificateStatusDesc(certificatePo.getCertificateStatus().getValue());

        // 统一社会信用代码
        resDto.setSocialCreditCode(certificatePo.getSocialCreditCode());
        // 所在国别地区
        resDto.setArea(certificatePo.getArea());
        // 本证书体系覆盖人数
        resDto.setCoverPeopleNumber(certificatePo.getCoverPeopleNumber());
        // 组织地址
        resDto.setOrganizationAddress(certificatePo.getOrganizationAddress());
        // 颁证日期
        resDto.setIssueDate(certificatePo.getIssueDate());
        // 初次获证日期
        resDto.setInitialDate(certificatePo.getInitialDate());
        // 监督次数
        resDto.setSuperviseTimes(certificatePo.getSuperviseTimes());
        // 认证项目认证依据
        resDto.setCertificationBasis(certificatePo.getCertificationBasis());
        // 认证覆盖的业务范围
        resDto.setBusinessScope(certificatePo.getBusinessScope());
        // 是否覆盖多场所
        resDto.setIsMultipleLocations(certificatePo.getIsMultipleLocations() ? "是" : "否");
        // 认证覆盖的场所名称及地址
        resDto.setMultipleLocationsAddress(certificatePo.getMultipleLocationsAddress());
        // 证书使用的认可标识
        resDto.setRecognitionMark(certificatePo.getRecognitionMark());
        // 再次认证次数
        resDto.setRecertificationTimes(certificatePo.getRecertificationTimes());

        return resDto;
    }

    @Override
    public void deleteCertificate(CertificateDeleteReqDto reqDto) {
        CertificatePo updatePo = new CertificatePo();
        updatePo.setSerialNo(reqDto.getSerialNo());
        certificateService.deleteBySerialNo(updatePo);
    }

    @Override
    public void updateCertificate(CertificateUpdateReqDto reqDto) {
        CertificatePo updatePo = new CertificatePo();
        updatePo.setSerialNo(reqDto.getSerialNo());
        updatePo.setCertificateNo(reqDto.getCertificateNo());
        updatePo.setOrganizationName(reqDto.getOrganizationName());
        updatePo.setOrganizationAddress(reqDto.getOrganizationAddress());
        updatePo.setCertificationProject(reqDto.getCertificationProject());
        updatePo.setCertificateStatus(CertificateStatusEnum.valueOf(reqDto.getCertificateStatus()));
        updatePo.setDueDate(reqDto.getDueDate());
        updatePo.setSocialCreditCode(reqDto.getSocialCreditCode());
        updatePo.setArea(reqDto.getArea());
        updatePo.setCoverPeopleNumber(reqDto.getCoverPeopleNumber());
        updatePo.setIssueDate(reqDto.getIssueDate());
        updatePo.setInitialDate(reqDto.getInitialDate());
        updatePo.setSuperviseTimes(reqDto.getSuperviseTimes());
        updatePo.setCertificationBasis(reqDto.getCertificationBasis());
        updatePo.setBusinessScope(reqDto.getBusinessScope());
        updatePo.setIsMultipleLocations(reqDto.getIsMultipleLocations());
        updatePo.setMultipleLocationsAddress(reqDto.getMultipleLocationsAddress());
        updatePo.setRecognitionMark(reqDto.getRecognitionMark());
        updatePo.setRecertificationTimes(reqDto.getRecertificationTimes());
        certificateService.updateCertificate(updatePo);
    }

}
